package com.zimi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zimi.model.WfDictionary;
import com.zimi.service.WfDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: lmc
 * @Date: 2019/3/27 10:34
 * @Description:
 */

@Api("流程模块")
@RestController
@RequestMapping("wf/dictionary")
public class WfDictionaryApi {

    private final WfDictionaryService wfDictionaryService;

    @Autowired
    public WfDictionaryApi(WfDictionaryService wfDictionaryService) {
        this.wfDictionaryService = wfDictionaryService;
    }

    @PostMapping("list")
    @ApiOperation("查询所有")
    public Map<String, Object> getAll(WfDictionary wfDictionary) {
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("OK", "OK");
        returnMap.put("code", 200);
        returnMap.put("rows", wfDictionaryService.selectAll(wfDictionary));
        return returnMap;
    }

    @PostMapping("parentList")
    @ApiOperation("查询所有父级")
    public Map<String, Object> getParentList(WfDictionary wfDictionary) {
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("OK", "OK");
        returnMap.put("code", 200);
        returnMap.put("rows", wfDictionaryService.selectWfDictionaryParent(wfDictionary));
        return returnMap;
    }

    @PostMapping("findById")
    @ApiOperation("根据Id查询信息")
    public Object selectByPrimaryKey(int id) {
        Map<String, java.io.Serializable> returnMap = new HashMap<>();

        returnMap.put("OK", "OK");
        returnMap.put("code", "200");
        returnMap.put("wfDictionary", wfDictionaryService.selectByPrimaryKey(id));
        return returnMap;
    }

    @PostMapping("del")
    @ApiOperation("根据Id删除")
    public Object deleteByPrimaryKey(int id) {
        Map<String, String> returnMap = new HashMap<String, String>();
        boolean flag = wfDictionaryService.deleteByPrimaryKey(id) > 0;
        if (flag) {
            returnMap.put("OK", "OK");
            returnMap.put("code", "200");
        } else {
            returnMap.put("OK", "error");
            returnMap.put("code", "500");
        }
        return returnMap;
    }

    @PostMapping("insert")
    @ApiOperation("添加")
    public Object insert(WfDictionary wfDictionary) {
        Map<String, String> returnMap = new HashMap<>();
        if (wfDictionary.getEnName() == null || "".equals(wfDictionary.getEnName())) {
            WfDictionary wd = new WfDictionary();
            wd.setDictionaryId(Integer.parseInt(wfDictionary.getParentId()));
            wd = wfDictionaryService.selectByPrimaryKey(Integer.parseInt(wfDictionary.getParentId()));
            wfDictionary.setEnName(wd.getEnName());
        }
        if (wfDictionary.getValue() == null || "".equals(wfDictionary.getValue())) {
            WfDictionary wd = new WfDictionary();
            wd.setParentId(wfDictionary.getParentId());
            String val = wfDictionaryService.selectMaxValue(wd);
            if (val == null || "".equals(val)) {
                val = "1";
            } else {
                val = Integer.toString(Integer.parseInt(val) + 1);
            }
            wfDictionary.setValue(val);
        }
        boolean flag = wfDictionaryService.insertSelective(wfDictionary) > 0;
        if (flag) {
            returnMap.put("OK", "OK");
            returnMap.put("code", "200");
        } else {
            returnMap.put("OK", "error");
            returnMap.put("code", "500");
        }
        return returnMap;
    }

    @PostMapping("insert/duplicate/removal")
    @ApiOperation("添加")
    public Object insertDuplicate(WfDictionary wfDictionary) {
        Map<String, String> returnMap = new HashMap<String, String>();
        try {
            boolean flag = wfDictionaryService.insertinsertDuplicate(wfDictionary);
            if (flag) {
                returnMap.put("OK", "OK");
                returnMap.put("code", "200");
            } else {
                returnMap.put("OK", "duplicate");
                returnMap.put("code", "500");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("OK", "error");
            returnMap.put("code", "500");
        }
        return returnMap;
    }

    @PostMapping("update")
    @ApiOperation("根据条件修改信息")
    public Object updateByPrimaryKey(WfDictionary wfDictionary) {
        Map<String, String> returnMap = new HashMap<>();
        boolean flag = wfDictionaryService.updateByPrimaryKeySelective(wfDictionary) > 0;
        if (flag) {
            returnMap.put("OK", "OK");
            returnMap.put("code", "200");
        } else {
            returnMap.put("OK", "error");
            returnMap.put("code", "500");
        }
        return returnMap;
    }


    @PostMapping("updateData")
    @ApiOperation("根据条件修改信息")
    public Object updateData(WfDictionary wfDictionary) {
        Map<String, String> returnMap = new HashMap<String, String>();
        wfDictionaryService.updateData(wfDictionary);
        returnMap.put("OK", "OK");
        returnMap.put("code", "200");
        return returnMap;
    }

    @PostMapping("update/byid")
    public void update(int dictionaryId, String value) {
        WfDictionary wfDictionary = new WfDictionary();
        wfDictionary.setDictionaryId(dictionaryId);
        wfDictionary.setValue(value);
        wfDictionaryService.updateData(wfDictionary);
    }

    @PostMapping("select/page")
    @ApiOperation("分页查询")
    public Object pageList(WfDictionary record, int page, int limit) {
        PageHelper.startPage(page, limit);
        List<WfDictionary> list = wfDictionaryService.selectAll(record);
        PageInfo<WfDictionary> pageInfo = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("total", pageInfo.getTotal());
        map.put("rows", list);
        return map;
    }

}
